package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ROLE_VALUE;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Enumeration extends DataType {

  public Enumeration() {
    this(null);
  }

  public Enumeration(String name) {
    super(name);
    values = new ArrayList<Value>();

    destructable = true;
    preserved = false;
  }

  public Enumeration(String name, Visibility visibility) {
    super(name, visibility);
    values = new ArrayList<Value>();

    destructable = true;
    preserved = false;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    Enumeration ne = (Enumeration) super.clone();
    ne.setName(name);

    ne.values = new ArrayList<Value>();
    Iterator<Value> iter = values.iterator();
    while (iter.hasNext()) {
      Value c = iter.next();
      ne.addValue((Value) c.clone());
    }
    return ne;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o instanceof Enumeration) {
      Enumeration e = (Enumeration) o;
      if (name == null) {
	return (e.name == null);
      } else {
	return name.equals(e.name);
      }
    }
    return false;
  }

  public List<Value> getValues() {
    return values;
  }

  public void addValue(Value f) {
    if (f != null) {
      values.add(f);
      addOwnedElement(f, ROLE_VALUE);
    }
  }

  public void removeValue(Value f) {
    if (f != null) {
      values.remove(f);
      removeOwnedElement(f);
    }
  }

  public void removeAllValues() {
    Iterator<Value> iter = values.iterator();
    while (iter.hasNext()) {
      Value f = iter.next();
      removeOwnedElement(f);
      iter.remove();
    }
  }

  protected List<Value> values;

}